package com.example.recordingpen_datacollection.Utils;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class HTTPJSON {

    public static String json(String result) {
        StringBuilder sb = new StringBuilder();
//        System.out.println(result);
        //data: {"Note":"以上内容为AI生成，不代表开发者立场，请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"作"}}],"Created":1701052235,"Id":"2b5ad935-f5e1-4809-bb0f-45e8772be177","Usage":{"PromptTokens":2,"CompletionTokens":1,"TotalTokens":3}}
        //data: {"Note":"以上内容为AI生成，不代表开发者立场，请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"为一个"}}],"Created":1701052235,"Id":"2b5ad935-f5e1-4809-bb0f-45e8772be177","Usage":{"PromptTokens":2,"CompletionTokens":2,"TotalTokens":4}}
        //data: {"Note":"以上内容为AI生成，不代表开发者立场，请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"人"}}],"Created":1701052236,"Id":"2b5ad935-f5e1-4809-bb0f-45e8772be177","Usage":{"PromptTokens":2,"CompletionTokens":3,"TotalTokens":5}}
        //data: {"Note":"以上内容为AI生成，不代表开发者立场，请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"工智"}}],"Created":1701052236,"Id":"2b5ad935-f5e1-4809-bb0f-45e8772be177","Usage":{"PromptTokens":2,"CompletionTokens":4,"TotalTokens":6}}
        //data: {"Note":"以上内容为AI生成，不代表开发者立场，请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"能模"}}],"Created":1701052236,"Id":"2b5ad935-f5e1-4809-bb0f-45e8772be177","Usage":{"PromptTokens":2,"CompletionTokens":5,"TotalTokens":7}}
        //data: {"Note":"以上内容为AI生成，不代表开发者立场，请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"型"}}],"Created":1701052236,"Id":"2b5ad935-f5e1-4809-bb0f-45e8772be177","Usage":{"PromptTokens":2,"CompletionTokens":6,"TotalTokens":8}}
        //data: {"Note":"以上内容为AI生成，不代表开发者立场，请勿删除或修改本标记","Choices":[{"FinishReason":"stop","Delta":{"Role":"assistant","Content":""}}],"Created":1701052240,"Id":"2b5ad935-f5e1-4809-bb0f-45e8772be177","Usage":{"PromptTokens":2,"CompletionTokens":195,"TotalTokens":197}}
        if (result.length() > 0) {
            String[] arr = result.split("data: ");
            for (String str : arr) {

                // Log.e(TAG, "Google onResponse: " + str);
                if (!str.startsWith("{")) {
                    continue;
                }

                try {
                    JSONObject jsonObject = new JSONObject(str);
                    JSONArray jsonArray = jsonObject.getJSONArray("Choices");// 返回的回复, 支持多个
                    if (jsonArray.length() > 0) {

                        JSONObject choice = jsonArray.getJSONObject(0);
                        String finishReason = choice.getString("FinishReason");
                        JSONObject delta = choice.getJSONObject("Delta");
                        String content = delta.getString("Content");

                        sb.append(content);

                        if ("stop".equals(finishReason)) {

                            // tokens 数量
                            JSONObject jsonTokens = jsonObject.getJSONObject("Usage");
                            int total_tokens = jsonTokens.getInt("TotalTokens");
                            int completion_tokens = jsonTokens.getInt("CompletionTokens");
                            int prompt_tokens = jsonTokens.getInt("PromptTokens");
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        }
        return sb.toString();
    }
}